package com.inibb.app.converter;

import com.inibb.app.po.security.User;
import com.inibb.app.vo.security.UserVO;




public class UserConverter extends DTOConverter<User, UserVO>{
	private RoleConverter roleConverter = new RoleConverter();
	
	public UserVO toDTO (User user) {
		if (null == user) return null;

		UserVO userDTO = new UserVO( );

		userDTO.setId( user.getId() );
		userDTO.setLoginName( user.getLoginName() );
		userDTO.setPassword( user.getPassword() );
		userDTO.setConfirmPassword( user.getPassword() );
		userDTO.setName( user.getName() );
		userDTO.setEmail( user.getEmail() );
		userDTO.setRoles( roleConverter.toDTOs( user.getRoles() ) );
		userDTO.setActiveFlag(user.getActiveFlag());
//		userDTO.setGroupId(StringUtil.toString(user.getGroupId()));
		userDTO.setEffectiveFrom(user.getEffectiveFrom());
		userDTO.setEffectiveTo(user.getEffectiveTo());
		userDTO.setRegDate(user.getRegDate());
		try {
			userDTO.setRoleNames(user.getRoleNames());
			userDTO.setRoleIds(user.getRoleIdsAsArray());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return userDTO;
	}



	public User toPO (UserVO userDTO) {
		if (null == userDTO) return null;

		User user = new User();

		user.setId( userDTO.getId() );
		user.setLoginName( userDTO.getLoginName() );
		user.setPassword( userDTO.getPassword() );
		user.setName( userDTO.getName() );
		user.setEmail( userDTO.getEmail() );
		user.setRoles( roleConverter.toPOs(userDTO.getRoles()) );
		user.setActiveFlag(userDTO.getActiveFlag());
		user.setEffectiveFrom(userDTO.getEffectiveFrom());
		user.setEffectiveTo(userDTO.getEffectiveTo());
		user.setRegDate(userDTO.getRegDate());		
		/*if(!StringUtil.isEmptyString(userDTO.getGroupId())){
			user.setGroupId(Long.valueOf(userDTO.getGroupId()));
		}*/

		return user;
	}


}
